const { model, Schema } = require('mongoose')
const md5 = require('md5')
const BaseModel = require('./baseModel')

const userSchema = Schema({
  username: {
    type: String,
    // 必填 
    required: [true, '用户名必须填写'],
    // 限制长度
    minLength: 2
  },
  password: {
    // select: false,
    type: String,
    // required: true,
    required: [true, '密码不能为空'],
    // 修改器
    set: v => md5(v),
    // 访问器
    get: v => '保密'
  },
  phone: {
    type: String,
    // 自定义验证器
    validate: {
      // 返回ture或false，false验证失败
      validator: function (v) {
        return /^1[3-9]\d{9}$/.test(v)
      },
      message: '手机号码不合法'
    }
  },
  sex: {
    type: Number,
    enum: [1, 2],
    default: 1
  },
  age: {
    type: Number,
    default: 1
  },
  nickname: String,
  avatar: String,
  intro: String,
  hobbies: [String],
  isdel: {
    type: Boolean,
    default: false
  }
}, {
  // mongoose它默认会提从一个_v来记录更新版本，如果你是老项目，则一般关闭它，新项目开启,提升性能
  // versionKey: false,
  timestamps: true
})

const userModel = model('User', userSchema)

class UserModel extends BaseModel {
  constructor() {
    super(userModel)
  }
}


module.exports = new UserModel()